<?php

namespace app\admin\controller;

use app\admin\model\Guide as GuideModel;
use app\BaseController;

class Guide extends BaseController
{
    /* 列表 */
    public function index()
    {
        if (!$this->request->auth) return show(config('status.auth'), 'Authorization Failed');
        $cid = input('cid', 0, 'intval');
        $page = input('page', 1, 'intval');
        $perpage = 10;
        $result = GuideModel::with('type')->order('sort asc')->page($page, $perpage)->where('cid', $cid)->select();
        $total = GuideModel::where('cid', $cid)->count();
        $data = [
            'total' => $total,
            'current' => $page,
            'perpage' => $perpage,
            'data' =>  $result
        ];
        return show(config('status.success'), '获取列表', $data);
    }

    /* 添加 */
    public function add()
    {
        if (!$this->request->auth) return show(config('status.auth'), 'Authorization Failed');
        $cid = input('cid', 0, 'intval');
        $title = input('title', '');
        $tid = input('type', 0, 'intval');
        $file = input('file', '');
        $sort = input('sort', 0, 'intval');
        $status = input('status', 0, 'intval');

        $result = GuideModel::create([
            'cid' => $cid,
            'title' => $title,
            'tid' => $tid,
            'file' => $file,
            'sort' => $sort,
            'status' => $status
        ]);
        if ($result) {
            return show(config('status.success'), '添加成功');
        } else {
            return show(config('status.error_insert'), '添加失败');
        }
    }

    /* 获取 */
    public function get()
    {
        if (!$this->request->auth) return show(config('status.auth'), 'Authorization Failed');
        $id = input('id', 0, 'intval');
        $cid = input('cid', 0, 'intval');
        $data = GuideModel::where(['id' => $id, 'cid' => $cid])->find();
        if ($data) {
            return show(config('status.success'), '查询成功', $data);
        } else {
            return show(config('status.error_find'), '查询失败');
        }
    }

    /* 修改 */
    public function modify()
    {
        if (!$this->request->auth) return show(config('status.auth'), 'Authorization Failed');
        $id = input('id', 0, 'intval');
        $cid = input('cid', 0, 'intval');
        $title = input('title', '');
        $tid = input('type', 0, 'intval');
        $file = input('file', '');
        $sort = input('sort', 0, 'intval');
        $status = input('status', 0, 'intval');

        $result = GuideModel::update([
            'id' => $id,
            'cid' => $cid,
            'title' => $title,
            'tid' => $tid,
            'file' => $file,
            'sort' => $sort,
            'status' => $status
        ]);
        if ($result) {
            return show(config('status.success'), '修改成功');
        } else {
            return show(config('status.error_update'), '修改失败');
        }
    }

    /* 切换 */
    public function switch()
    {
        if (!$this->request->auth) return show(config('status.auth'), 'Authorization Failed');
        $id = input('id', 0, 'intval');
        $status = input('status', 0, 'intval');

        $result = GuideModel::update([
            'id' => $id,
            'status' => $status
        ]);
        if ($result) {
            return show(config('status.success'), '状态切换成功');
        } else {
            return show(config('status.error_switch'), '状态切换失败');
        }
    }

    /* 删除 */
    public function delete()
    {
        if (!$this->request->auth) return show(config('status.auth'), 'Authorization Failed');
        $id = input('id', 0, 'intval');
        $result = GuideModel::destroy($id);
        if ($result) {
            return show(config('status.success'), '删除成功');
        } else {
            return show(config('status.error_delete'), '删除失败');
        }
    }

    /* 批量删除 */
    public function deleteAll()
    {
        if (!$this->request->auth) return show(config('status.auth'), 'Authorization Failed');
        $id = input('id', '');
        $arr = explode(',', $id);
        $result = GuideModel::destroy($arr);
        if ($result) {
            return show(config('status.success'), '删除成功');
        } else {
            return show(config('status.error_delete'), '删除失败');
        }
    }
}
